package com.xzr.spring;

import com.xzr.test.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MultitonFactory {
    public Person getBean(String key){
        //1.读取配置文件
        Properties properties = new Properties();
        InputStream resourceAsStream = MultitonFactory.class.getResourceAsStream("beans.properties");
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String str:properties.stringPropertyNames()){
            if(key.equals(str)){
                Person person = null;
                try {
                    person = (Person) Class.forName(properties.getProperty(key)).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return person;
            }
        }
        return null;
    }
}



